/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.DataRetrieval;

import Utilities.Constants.MiscellaneousRequestType;
import Utilities.Constants.ResourceRequestType;
import Utilities.Functions;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to
 * retrieve various data is sent to the data retrieval service.
 *
 * @see MiscellaneousRequestType data request types
 *
 * @author Angel Sanadinov
 */
public class MiscDataRetrievalRequest extends LocalResourceRequest
{
    private MiscellaneousRequestType requestType;

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public MiscDataRetrievalRequest()
    {
        super();
        setRequestedAction(PermissionsConstants.ACTION_RETRIEVE_MISC);
    }

    /**
     * Constructs a data request with the basic required information. <br>
     *
     * <b>Note:</b> <i>The request type must be explicitly set.</i>
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server with which the data is associated
     */
    public MiscDataRetrievalRequest(int requestorId, int serverId)
    {
        super(requestorId, serverId);
        this.requestType = MiscellaneousRequestType.INVALID_REQUEST;
        setRequestedAction(PermissionsConstants.ACTION_RETRIEVE_MISC);
    }

    /**
     * Constructs a data request with the supplied information.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server with which the data is associated
     * @param requestType the type of data being requested
     */
    public MiscDataRetrievalRequest(int requestorId, int serverId, MiscellaneousRequestType requestType)
    {
        this(requestorId, serverId);
        this.requestType = requestType;
    }

    /**
     * Retrieves the data request type.
     *
     * @return the request type
     */
    public MiscellaneousRequestType getRequestType()
    {
        return requestType;
    }

    /**
     * Sets the data request type.
     *
     * @param requestType the request type
     */
    public void setRequestType(MiscellaneousRequestType requestType)
    {
        this.requestType = requestType;
    }

    @Override
    protected boolean isResourceIdValid()
    {
        return Functions.isServerIdValid(getResourceId());
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && getResourceRequestType() == ResourceRequestType.SINGLE_RESOURCE
                && requestType != null && requestType != MiscellaneousRequestType.INVALID_REQUEST)
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (resourceId,requestType)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return MiscDataRetrievalRequest.class.getSimpleName()
                + ": (" + getResourceId() + "," + requestType + ")";
    }
}
